﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using TierService;
using OrderMgr.SysSetting;
using TierApplication;

namespace OrderMgr.SysSetting
{
    public partial class frmProduct : TierService.frmTemplate
    {
        public string _ProductID;

        public frmProduct()
        {
            InitializeComponent();
        }

        private void initForm()
        {
            Utility.DataToLookupComboxEx(txtProductType, C_DMB.GetListByType(C_DMB.DType.ProductType.ToString()), "CodeID", "CodeName");

            TierService.Utility.gFuncID = "iProduct";
            TierService.Utility.IniInterface(this, "iProduct");

            this.Text = "商品管理";
            if (gFuncType == Utility.FuncType.Select)
            {
                this.Text = "商品管理";
                btnAdd.Visible = false;
                btnModify.Visible = false;
                btnDelete.Visible = false;
                Utility.DataToLookupComboxEx(txtProductType, C_DMB.GetListByType(C_DMB.DType.ProductType.ToString()), "CodeID", "CodeName");
            }

            //Utility.DataToLookupComboxEx(txtProductType, C_DMB.GetListByType(C_DMB.DType.ProductType.ToString()), "CodeID", "CodeName");
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            gvCustomer.RowHeight = 160;
            string ls_where = "";

            if ((txtProductID.Text.Trim() != "") && (ckbProductID.Checked))
                ls_where = ls_where + " and ProductID  like '%" + txtProductID.Text + "%'";

            if (gProductType == Utility.ProductType.MProduct)
                ls_where = ls_where + " and ProductProp  ='1'";
            if (gProductType == Utility.ProductType.Product)
                ls_where = ls_where + " and ProductProp  ='2'";

            if ((txtProductName.Text.Trim() != "") && (ckbProductName.Checked))
                ls_where = ls_where + " and ProductName  like '%" + txtProductName.Text + "%'";

            if ((txtProductType.Text.Trim() != "") && (ckbProductType.Checked))
                ls_where = ls_where + " and ProductType = '" + (string)txtProductType.EditValue + "'";

            gvCustomer.RowHeight = 0;

            //if (ceFbqj.Checked)
            //{
            //    ls_where = ls_where + " and  DATEDIFF(year,a.Birthday,a.HspDateIn) >= '" + txtFbqjMin.Value + "'";
            //    ls_where = ls_where + " and  DATEDIFF(year,a.Birthday,a.HspDateIn) <= '" + txtFbqjMax.Value + "'";
            //}

            //if ((ceHspDateIn.Checked) && (txtHspDateInMin.Text != "") && (txtHspDateInMax.Text != ""))
            //{
            //    ls_where = ls_where + " and a.HspDateIn>='" + txtHspDateInMin.Text + "' and a.HspDateIn <='" + txtHspDateInMax.Text + "'";
            //}

            ckbAll.Checked = false;
            T_Product mProduct = new T_Product();
            DataSet dsProduct;

            dsProduct = mProduct.GetProductList(ls_where,"");
            gcCustomer.DataSource = dsProduct.Tables[0];
            gvCustomer.ExpandAllGroups();
        }

        private void btnExportExcel_Click(object sender, EventArgs e)
        {
            TierService.Utility.DataExportToExcel(gvCustomer, "商品信息");
        }


        private void gvCustomer_DoubleClick(object sender, EventArgs e)
        {
            if (gFuncType == Utility.FuncType.Select)
            {
                if (gvCustomer.RowCount < 1) return;
                DataRow dr = gvCustomer.GetDataRow(gvCustomer.FocusedRowHandle);
                _ProductID = dr["ProductID"].ToString();
                DialogResult = System.Windows.Forms.DialogResult.OK;
            }
            else
            {
                btnQuery_Click(null, null);
            }
        }


        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (gvCustomer.RowCount < 1) return;
            if (MessageBox.Show("是否要删除此商品记录，删除后将无法恢复!", "确认", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
                return;
            DataRow dr = gvCustomer.GetDataRow(gvCustomer.FocusedRowHandle);
            string sID = dr["ProductID"].ToString();

            T_Product mProduct = new T_Product();
            mProduct.Delete(sID);

            btnSearch_Click(null, null);
        }


        private void gvCustomer_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            //e.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Far;
            //if (e.Info.IsRowIndicator)
            //{
            //    if (e.RowHandle >= 0)
            //    {
            //        e.Info.DisplayText = (e.RowHandle + 1).ToString();
            //    }
            //    else if (e.RowHandle < 0 && e.RowHandle > -1000)
            //    {
            //        e.Info.Appearance.BackColor = System.Drawing.Color.AntiqueWhite;
            //        e.Info.DisplayText = "G" + e.RowHandle.ToString();
            //    }
            //}
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            frmProductDetail fDetail = new frmProductDetail();
            fDetail.OpMode = TierService.Utility.OperateMode.Insert;
            fDetail.gProductType = Utility.ProductType.Product;
            fDetail.ShowDialog();

            btnSearch_Click(null, null);
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            if (gvCustomer.RowCount < 1) return;
            DataRow dr = gvCustomer.GetDataRow(gvCustomer.FocusedRowHandle);
            string sID = dr["ProductID"].ToString();
            string sProductType = dr["ProductProp"].ToString();

            frmProductDetail fDetail = new frmProductDetail();
            fDetail.OpMode = TierService.Utility.OperateMode.Update;
            fDetail._ProductID = sID;
            if (fDetail.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {

            }
            string bookmark = sID;
            btnSearch_Click(null, null);


            if (bookmark != "")
            {
                for (int i = 0; i < this.gvCustomer.RowCount; i++)
                {
                    string CellText = this.gvCustomer.GetRowCellDisplayText(i, "ProductID");
                    if (CellText == bookmark)
                    {
                        this.gvCustomer.FocusedRowHandle = i;
                        this.gvCustomer.MoveBy(i - 1);
                        break;
                    }
                }
            }
        }

        private void frmProduct_Load(object sender, EventArgs e)
        {
            initForm();

        }

        private void gvCustomer_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            if (gvCustomer.RowCount < 1) return;
            DataRow dr = gvCustomer.GetDataRow(gvCustomer.FocusedRowHandle);
            //dr["IsChecked"] = (dr["IsChecked"] == "1") ? "0" : "1";
        }

        private void ceAll_CheckedChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < gvCustomer.DataRowCount; i++)
            {
                DataRow dr = gvCustomer.GetDataRow(i);
                dr["IsChecked"] = ckbAll.Checked ? "1" : "0";
            }
        }


        private int GetSelectRowCount()
        {
            int iCount = 0;
            for (int i = 0; i < gvCustomer.DataRowCount; i++)
            {
                DataRow dr = gvCustomer.GetDataRow(i);
                if (dr["IsChecked"] == "1")
                {
                    iCount = iCount + 1;
                }
            }
            return iCount;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            if (gvCustomer.RowCount < 1) return;
            DataRow dr = gvCustomer.GetDataRow(gvCustomer.FocusedRowHandle);
            string sID = dr["ProductID"].ToString();
            string sProductType = dr["ProductProp"].ToString();

            frmProductDetail fDetail = new frmProductDetail();
            fDetail.OpMode = TierService.Utility.OperateMode.Query;
            if (sProductType == "1")
                fDetail.gProductType = Utility.ProductType.MProduct;
            else
                fDetail.gProductType = Utility.ProductType.Product;

            fDetail._ProductID = sID;
            fDetail.ShowDialog();
        }


    }
}